//
//  DLMachineInfoTC.m
//  delongzhitou
//
//  Created by Mr.Wang on 2018/11/28.
//  Copyright © 2018 delongzhitou. All rights reserved.
//

#import "DLMachineInfoTC.h"
#import "DLMachineModel.h"

@interface DLMachineInfoTC()

@property (nonatomic, strong) UIView *infoBgView;

@property (nonatomic, strong) UILabel *noDataLabel;
@end

@implementation DLMachineInfoTC

- (instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self layoutsubControls];
        self.backgroundColor = kDLMachineTableAndCellBgColor;
//        self.backgroundColor = kColorRGB(240, 240, 240);
//        self.backgroundColor = kColorRandom;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}
- (void) layoutsubControls {
    
    
    UIView *infoBgView = [UIView new];
    infoBgView.frame = CGRectMake(kMachineInfoBgViewLeftMargin, kMachineInfoBgViewTopMargin, kScreenWidth - kMachineInfoBgViewLeftMargin * 2, kMachineInfoViewDefaultHeight - 40);
    infoBgView.backgroundColor = kDLMachineTableAndCellBgColor;
    infoBgView.backgroundColor = kColorRandom;
    infoBgView.backgroundColor = [UIColor whiteColor];
    infoBgView.tag = 1000;
    [self.contentView addSubview:infoBgView];
    self.infoBgView = infoBgView;
    
    
    
    UIButton *showAllButton = [UIButton buttonWithType:0];
    showAllButton.frame = CGRectMake(12 + kMachineInfoBgViewLeftMargin, infoBgView.bottom, infoBgView.width - 24, kMachineInfoBottomBtnHeight);
    [showAllButton setTitle:@"展开全部" forState:UIControlStateNormal];
    [showAllButton setTitle:@"收起" forState:UIControlStateSelected];
    showAllButton.titleLabel.font = kSystemFont(13);
    showAllButton.backgroundColor = kDLBtnNormalBgColor;
    [showAllButton addTarget:self action:@selector(showAllButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:showAllButton];
    self.showAllButton = showAllButton;
    
    UILabel *noDataLabel = [UILabel new];
    noDataLabel.frame = CGRectMake(kMachineInfoBgViewLeftMargin, kMachineInfoBgViewTopMargin + 17, kScreenWidth - kMachineInfoBgViewLeftMargin *2, kMachineInfoViewDefaultHeight - kMachineInfoBgViewTopMargin *2 - 20 - 17*2);
    noDataLabel.text = @"暂无数据";
    noDataLabel.textAlignment =NSTextAlignmentCenter;
    noDataLabel.font = kSystemFont(30);
    noDataLabel.textColor = kDLNormalBlackColor;
    noDataLabel.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:noDataLabel];
    self.noDataLabel = noDataLabel;
    noDataLabel.hidden = YES;
}
- (void) showAllButtonClick:(UIButton *) button {
    button.selected = !button.selected;
    self.infoModel.isInfoShowAllData = !self.infoModel.isInfoShowAllData;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"plainTableShouldReloadData" object:nil];
}

- (void) setInfoModel:(DLMachineModel *)infoModel {
    _infoModel = infoModel;
    
    //新加的尝试
    if (infoModel.isInfoShowAllData == YES) {
        self.showAllButton.selected = YES;
    } else {
        self.showAllButton.selected = NO;
    }
    
    [self.infoBgView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    NSArray *dataArr = infoModel.resultDic[@"data"];
    
    NSLog(@"资讯的数据是：%ld", dataArr.count);
    NSMutableArray *showAllDataSource = [NSMutableArray array];
    
    
    
    if (dataArr.count > 0) {
        self.noDataLabel.hidden = YES;
        if (dataArr.count < 9) {
            self.showAllButton.hidden = YES;
            showAllDataSource = [NSMutableArray arrayWithArray:dataArr];
            
            [self.infoBgView resetHeight:showAllDataSource.count *kMachineInfoTitleLabelHeight];
        } else {
            self.showAllButton.hidden = NO;
            
            for (NSInteger i = 0; i < dataArr.count; i++) {
                
                NSArray *dic = dataArr[i];
                if (i == 5) {
                    if (infoModel.isInfoShowAllData == YES) {
                        [showAllDataSource addObject:dic];
                        continue;
                    } else {
                        break;
                    }
                }
                [showAllDataSource addObject:dic];
            }
            
            [self.infoBgView resetHeight:showAllDataSource.count *kMachineInfoTitleLabelHeight + kMachineInfoBottomBtnHeight + 20];
            [self.showAllButton resetY:self.infoBgView.height + kMachineInfoBgViewTopMargin - 20 - kMachineInfoBottomBtnHeight];
        }
        
        for (NSInteger i = 0; i < showAllDataSource.count; i++) {
            
            NSDictionary *dic = showAllDataSource[i];
            NSString *rankStr = [NSString stringWithFormat:@"NO.%ld", i + 1];
            NSString *titleStr = [NSString stringWithFormat:@"%@", dic[@"title"]];
            
            UILabel *leftRankLabel = [UILabel new];
            leftRankLabel.frame = CGRectMake(0, kMachineInfoTitleLabelHeight *i, 60, kMachineInfoTitleLabelHeight);
            leftRankLabel.font = kSystemFont(14);
            leftRankLabel.textAlignment = NSTextAlignmentCenter;
            leftRankLabel.textColor = kColorRGB(85, 85, 85);
            leftRankLabel.text = rankStr;
            [self.infoBgView addSubview:leftRankLabel];
            
            UILabel *titleLabel = [UILabel new];
            titleLabel.frame = CGRectMake(leftRankLabel.right + 10, leftRankLabel.top, kAutoWidth(270), leftRankLabel.height);
            titleLabel.font = kSystemFont(14);
            titleLabel.textAlignment = NSTextAlignmentLeft;
            titleLabel.textColor = kDLNormalBlackColor;
            titleLabel.text = titleStr;
            titleLabel.numberOfLines = 2;
            titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
            titleLabel.userInteractionEnabled = YES;
            
            titleLabel.tag = 333 + i;
            [self.infoBgView addSubview:titleLabel];
            
            UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(titleLabelTap:)];
            [titleLabel addGestureRecognizer:tapGes];
            
            UIView *lineView = [UIView new];
            lineView.frame = CGRectMake(titleLabel.left, titleLabel.bottom, titleLabel.width - kAutoWidth(20), 1);
            lineView.backgroundColor = kColorRGB(232, 232, 232);
            [self.infoBgView addSubview:lineView];
            
            if (i == 0) {
                leftRankLabel.textColor = [UIColor redColor];
            }
            if (i == 1) {
                leftRankLabel.textColor = kColorRGB(255, 116, 0);
            }
            if (i == 2) {
                leftRankLabel.textColor = kColorRGB(255, 186, 0);
            }
        }
        
    } else {
        NSLog(@"显示无数据");
        self.noDataLabel.hidden = NO;
        self.showAllButton.hidden = YES;
    }
    
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.infoBgView.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerTopRight | UIRectCornerBottomRight cornerRadii:CGSizeMake(17, 17)];
    
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = maskPath.bounds;
    maskLayer.path = maskPath.CGPath;
    self.infoBgView.layer.mask  = maskLayer;
}
- (void) titleLabelTap:(UITapGestureRecognizer *)tapGes {
    
    NSInteger index = tapGes.view.tag - 333;
    
    NSArray *infoArr = self.infoModel.resultDic[@"data"];
    NSDictionary *dic = infoArr[index];
    NSString *infoId = dic[@"id"];
    
    DLNormalWebVC *bvc = [DLNormalWebVC new];
    bvc.urlString = HTTPInfoDetail;
    bvc.newsId = infoId;
    bvc.titleString = @"详情";
    bvc.webViewType = DLWebViewType_InfoDetail;
    [kuserManager.lastVc.navigationController pushViewController:bvc animated:YES];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
